import SnakeCtrl from "../../ctrl/SnakeCtrl";

const { ccclass, property } = cc._decorator;

@ccclass
export default class UISprint extends cc.Component {

    activated = false

    @property(cc.SpriteFrame)
    activatedFrame: cc.SpriteFrame = null
    @property(cc.SpriteFrame)
    normalFrame: cc.SpriteFrame = null

    start() {

        let node = this.node
        let sprite = node.getComponent(cc.Sprite)
        let that = this

        let width = this.node.width

        /** 激活 */
        function activate(e) {
            //设置为激活状态
            that.activated = true
            sprite.spriteFrame = that.activatedFrame
            that.node.width = that.node.height = width * 0.95
            SnakeCtrl.ins.ctrlSprint(that.activated)
        }

        /** 结束 */
        function done() {
            that.activated = false
            sprite.spriteFrame = that.normalFrame
            that.node.width = that.node.height = width
            SnakeCtrl.ins.ctrlSprint(that.activated)
        }

        node.on(cc.Node.EventType.TOUCH_START, activate, this);
        node.on(cc.Node.EventType.TOUCH_MOVE, activate, this);
        node.on(cc.Node.EventType.TOUCH_END, done, this);
        node.on(cc.Node.EventType.TOUCH_CANCEL, done, this);
    }

}
